﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace Dapper_Core.Common
{
    public class EncryptUtility
    {
        /// <summary>
        /// MD5加密（不可逆加密）
        /// </summary>
        /// <param name="pass">要加密的原始字串</param>
        /// <returns></returns> 
        public static string MD5Encrypt(string pass)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytResult = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(pass));
            md5.Clear();
            string strResult = BitConverter.ToString(bytResult);
            strResult = strResult.Replace("-", "");
            return strResult;
            /* 另一种方法
            pass = FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "MD5");
            return pass;
            */
        }

        /// <summary>
        /// SHA1加密（不可逆加密）
        /// </summary>
        /// <param name="pass">要加密的原始字串</param>
        /// <returns></returns> 
        public static string SHA1Encrypt(string pass)
        {
            System.Security.Cryptography.SHA1 sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
            byte[] bytResult = sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(pass));
            sha1.Clear();
            string strResult = BitConverter.ToString(bytResult);
            strResult = strResult.Replace("-", "");
            return strResult;
            /* 另一种方法
            pass = FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "SHA1");
            return pass;
            */
        }

        /// <summary>
        /// 创建随机数
        /// </summary>
        /// <param name="letterLength">字母个数</param>
        /// <param name="numLength">数字个数</param>
        /// <returns></returns>
        public static string CreateRandom(int letterLength, int numLength)
        {
            string result = CreateRandomLetter(letterLength) + CreateRandomNum(numLength);
            var temp = result.OrderBy(x => Guid.NewGuid()).ToArray(); //按一个随机值排序后组合成数组
            return new string(temp);
        }

        /// <summary>
        /// 创建字母随机数
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        private static string CreateRandomLetter(int length)
        {
            string str = "abcdefghijklmnopqrstuvwxyz";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                sb.Append(str[new Random(Guid.NewGuid().GetHashCode()).Next(0, str.Length - 1)]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 创建数字随机数
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        private static string CreateRandomNum(int length)
        {
            string str = "0123456789";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                sb.Append(str[new Random(Guid.NewGuid().GetHashCode()).Next(0, str.Length - 1)]);
            }
            return sb.ToString();
        }
    }
}
